package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.HsmanDAO;
import org.jsbd.boss.dao.IDeviceConfigDAO;
import org.jsbd.boss.domian.channel.ApkDevice;
import org.jsbd.boss.domian.channel.Hsman;
import org.jsbd.boss.service.IDeviceService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class DeviceServiceImpl implements IDeviceService {
	public static final Logger LOGGER = LoggerFactory.getLogger(DeviceServiceImpl.class);

	@Resource
	private HsmanDAO hsmanDao;

	@Resource
	private IDeviceConfigDAO deviceConfigDAO;

	@Resource
	private RedisClient redisClient;

	@Override
	public List<Hsman> selectList(String mark) {
		return hsmanDao.selectAll();
	}

	private Map<String, ApkDevice> initHsmanMap(List<ApkDevice> list) {
		Map<String, ApkDevice> map = new HashMap<String, ApkDevice>();
		for (ApkDevice device : list) {
			map.put(device.getHsman(), device);
		}
		return map;
	}


	@Override
	public List<String> selectHsmanList() {
		List<String> hsmanList = (List<String>) redisClient.get(CacheKey.HSMAN_LIST);
		if (hsmanList != null) {
			return hsmanList;
		}
		hsmanList = hsmanDao.selectHsman();
		if (hsmanList != null && hsmanList.size() > 0) {
			redisClient.put(CacheKey.HSMAN_LIST, hsmanList, 300);
		}
		return hsmanList;
	}

	@Override
	public List<ApkDevice> selectDevices(Integer apkId) {
		List<ApkDevice> targetList = new ArrayList<ApkDevice>();
		List<ApkDevice> list = deviceConfigDAO.selectDevices(apkId);
		Map<String, ApkDevice> map = initHsmanMap(list);
		List<String> hsmanList = hsmanDao.selectHsman();
		for (String hsman : hsmanList) {
			ApkDevice device = new ApkDevice();
			device.setApkId(apkId);
			device.setHsman(hsman);
			ApkDevice tempDevice = map.get(hsman);
			if (tempDevice != null) {
				device.setEnable(tempDevice.getEnable());
				device.setRate(tempDevice.getRate());
			} else {
				device.setEnable(0);
				device.setRate(0d);
			}
			targetList.add(device);
		}
		return targetList;
	}

	@Override
	public List<ApkDevice> selectDevicesOfCache(Integer apkId) {
		String key = CacheKey.A_DEVICE + apkId;
		List<ApkDevice> apkDeviceList = (List<ApkDevice>) redisClient.get(key);
		if (apkDeviceList != null) {
			return apkDeviceList;
		} else {
			if (redisClient.evalSetNx(CacheKey.A_DEVICE + apkId + "_lock", 10)) {
				apkDeviceList = deviceConfigDAO.selectDevices(apkId);
				if (apkDeviceList != null && apkDeviceList.size() > 0) {
					redisClient.put(CacheKey.A_DEVICE + apkId, apkDeviceList, 300);
				} else {
					redisClient.put(CacheKey.A_DEVICE + apkId, new ArrayList(), 300);
				}
			}
		}
		return apkDeviceList;
	}

	@Override
	public void insertApkDevices(List<ApkDevice> deviceList) {
		deviceConfigDAO.insertApkDevices(deviceList);
	}

	@Override
	public Integer delApkDevices(Integer apkId) {
		return deviceConfigDAO.delApkDevices(apkId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IDeviceService#insertApkDevice(org.jsbd.boss.domian
	 * .channel.ApkDevice)
	 */
	@Override
	public void insertApkDevice(ApkDevice device) {
		deviceConfigDAO.insertDevice(device);
	}

}
